﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace BBMS.DLL.DataContract
{
    public enum TrangThaiThanhToan
    {
        ChuaTT = 0,
        DaTT = 1,
        DaTTMotPhan = 2
    }

    public class BillHelper
    {
        public static DateTime TinhNgayThanhToanDuKien(string maNCC, DateTime ngay)
        {
            DateTime retDate = ngay;

            NhaCungCap ncc = (NhaCungCap)ObjectLists.NhaCungCapList.Get(maNCC);
            if (ncc != null)
            {
                if (ncc.PhuongThucTT == PhuongThucThanhToan.TheoThoiHan)
                {
                    retDate = retDate.AddDays(ncc.ThoiHanTT);
                }
                else
                {
                    // tinh toan ngay theo phuong thuc thanh toan
                    string[] strs = ncc.ThoiDiemTT.Split(',');
                    ArrayList arDates = new ArrayList();
                    for (int i = 0; i < strs.Length; i++)
                    {
                        int day = CommonTools.Helper.StringToInt(strs[i]);
                        if (day > 0 && day <= 31)
                        {
                            DateTime date = ngay;
                            int month = date.Month;
                            if (date.Day >= day) month += 1;

                            if (DateTime.DaysInMonth(date.Year, date.Month) < day)
                            {
                                day = 1;
                                month += 1;
                            }
                            date = date.AddDays(day - ngay.Day);
                            date = date.AddMonths(month - ngay.Month);
                            arDates.Add(date);
                        }
                    }
                    arDates.Sort();
                    if (arDates.Count > 0) retDate = (DateTime)arDates[0];
                }
            }
            return retDate;
        }
    }
}
